using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Example.Business;
using NBusiness.Data;

namespace www
{
    public partial class Update_FormView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                Beer b = (Beer)e.Row.DataItem;

                LinkButton selectLinkBtn = (LinkButton)e.Row.FindControl("SelectToUpdate");
                HyperLink availabilityLink = (HyperLink)e.Row.FindControl("AvailabilityLink");
                HyperLink styleLink = (HyperLink)e.Row.FindControl("StyleLink");
                HyperLink companyLink = (HyperLink)e.Row.FindControl("CompanyLink");

                selectLinkBtn.Visible = b.CanUpdate();
                availabilityLink.Text = b.Availability.Text;
                styleLink.Text = b.Style.Name;
                companyLink.Text = b.Company.Name;
            }
        }
        protected void BeerDataSource_Select(object sender, NBusiness.Data.Web.SelectEntityEventArgs e)
        {
            e.Entities = BeerCollection.FetchAll();
        }
        protected void AvailDataSource_Select(object sender, NBusiness.Data.Web.SelectEntityEventArgs e)
        {
            e.Entities = CachedAvailabilityCollection.FetchAll();
        }
        protected void BeerStyleDataSource_Select(object sender, NBusiness.Data.Web.SelectEntityEventArgs e)
        {
            e.Entities = CachedBeerStyleCollection.FetchAll();
        }
        protected void CompanyDataSource_Select(object sender, NBusiness.Data.Web.SelectEntityEventArgs e)
        {
            e.Entities = CompanyCollection.FetchAll();
        }
        protected void BeerItemDataSource_Select(object sender, NBusiness.Data.Web.SelectEntityEventArgs e)
        {
            Beer b = Beer.Fetch(Convert.ToInt32(GridView1.SelectedValue));
            e.Entities = new EntityCollection(b);
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Label1.Text = "GridView1.SelectedValue: " + GridView1.SelectedValue.ToString();
            FormView1.DataSourceID = "BeerDS";
            FormView1.DataBind();
            FormView1.Visible = true;
            FormView1.Enabled = true;
        }
        protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            if (e.CommandName == "Cancel")
            {
                FormView1.Visible = false;
                FormView1.Enabled = false;
                GridView1.SelectedIndex = -1;
            }
        }
        protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            Beer b = Beer.Fetch(Convert.ToInt32(e.Keys["BeerId"]));
            b.AvailabilityId = Convert.ToInt32(e.NewValues["AvailabilityId"]);
            b.Alcohol = Convert.ToDecimal(e.NewValues["Alcohol"]);
            b.BeerStyleId = Convert.ToInt32(e.NewValues["BeerStyleId"]);
            b.CompanyId = Convert.ToInt32(e.NewValues["CompanyId"]);
            b.Name = Convert.ToString(e.NewValues["Name"]);

            b.Save();
            GridView1.DataBind();

            FormView1.Visible = false;
            FormView1.Enabled = false;
            GridView1.SelectedIndex = -1;
        }
    }
}